package barbershop.com.service.impl;

import barbershop.com.dao.VipDao;
import barbershop.com.entity.Vip;
import barbershop.com.service.VipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class VipServiceImpl implements VipService {
    @Autowired
    private VipDao vipDao;

    @Override
    public Vip insert(Vip record) {
        Vip vip = new Vip();
        try {
            System.out.println(record);
            vip = this.vipDao.save(record);
            System.out.println("插入成功！");
            return vip;
        }  catch (Exception e){
            System.out.println("插入失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Boolean delete(Vip record) {
        try {
            this.vipDao.delete(record);
            System.out.println("删除成功！");
            return true;
        }   catch (Exception e){
            System.out.println("删除失败！");
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Vip update(Vip record) {
        Vip vip = new Vip();
        try {
            vip = this.vipDao.save(record);
            System.out.println("修改成功！");
            return vip;
        }   catch (Exception e){
            System.out.println("修改失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Vip find(Vip record) {
        Optional<Vip> vip;
        try {
            vip = this.vipDao.findById(record.getVipId());
            if (vip.isPresent()){
                System.out.println("查找成功！");
                return vip.get();
            }
            return null;
        }   catch (Exception e){
            System.out.println("查找失败！");
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Vip> getAll() {
        try {
            List<Vip> vipList = this.vipDao.findAll();
            if (vipList!=null){
                System.out.println("所有Vip数据获取成功！");
                return vipList;
            }
            return null;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("所有Vip数据获取失败！");
        }
        return null;
    }

    @Override
    public Page<Vip> getPage(Integer page, Integer size) {
        try {
            Pageable pageable = PageRequest.of(page-1, size);
            Page<Vip> vipPage = this.vipDao.findAll(pageable);
            System.out.println("所有Vip用户数据分页完成！");
            return vipPage;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("分页失败！");
        }
        return null;
    }

    @Override
    public Vip findByTelNum(String telNumber) {
        try {
            Vip vip = this.vipDao.findByVipPhonenumber(telNumber);
            System.out.println("找到VIP会员："+vip.getVipName());
            return vip;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("该会员不存在！");
        }
        return null;
    }

    @Override
    public void updateBalance(Double newBalance, Integer vipId) {
        try {
            Double balance = this.vipDao.findById(vipId).get().getVipBalance();
            newBalance += balance;
            this.vipDao.updateBalance(newBalance, vipId);
            System.out.println("VIP余额更新成功！剩余"+newBalance+"元");
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("VIP余额更新失败！");
        }
    }

    @Override
    public List<Vip> findVipByNameLike(String vipName) {
        try {
            List<Vip> vipList = this.vipDao.findVipByNameLike(vipName);
            System.out.println("VIP模糊查询成功！");
            return vipList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("VIP模糊查询失败！");
        }
        return null;
    }
}
